//var server = require('http').createServer();
const Server = require('socket.io');

try {
    var port = 8000 + Number(process.env.NODE_APP_INSTANCE || 0);
    console.log("port:" + port);
    const io = new Server(port);

    io.on('connection', function (socket) {
        console.log(port);
        // console.log("fdffddf");
        // // io.emit('this', { will: 'be received by everyone' });

        socket.on('private message', function (from, msg) {
            console.log('I received a private message by ', from, ' saying ', msg);
        });

        socket.on('chat message', function (from,msg) {
            console.log(port);
            console.log('I received a private message by ', from, ' saying ', msg);
            io.emit('chat message', from);
        });


        socket.on('disconnect', function () {
            io.emit('user disconnected');
        });
    });
    // server.listen(port, function (err) {
    //     if (err) {
    //         console.log(err);
    //     } else {
    //         console.log(`端口${port}启动成功`)
    //     }
    // })
} catch (e) {
    console.log(e);
}